var Transfer = (function($) {

	var userIdModel = {
		name : 'user_id',
		index : 'user_id',
		width : 60,
		sorttype : "int",
		hidden : true
	};

	var usernameModel = {
		name : 'username',
		index : 'username',
		width : 60,
		sorttype : "string"
	};

	var transTypeModel = {
		name : 'trans_type',
		index : 'trans_type',
		width : 60,
		sorttype : "string",
		align:'center'
	};

	var amountModel = {
		name : 'amount',
		index : 'amount',
		width : 80,
		align : "right",
		sorttype : "float",
		formatter : 'currency'
	};
	var transDateModel = {
		name : 'trans_date',
		index : 'trans_date',
		width : 100,
		sorttype : "date",
		formatter : 'date',
		align:'center'
	};

	return {

		init : function() {
			$("body").on("click", ".transfer-button", function() {
				Transfer.transfer();

			});
		},
		initTransferReport : function() {
			Report.initDateRangePicker();
			
			$("#submit").click(function() {
				Transfer.bindTransferReport();
				return false;
			});
		},
		bindTransferReport : function() {
			var gridOption = $.extend(Report.gridOption, {
				height : 500,
				colNames : [ 'User Id', "User", 'TransferType', 'Amount',
						'Date' ],
				colModel : [ userIdModel, usernameModel, transTypeModel,
						amountModel, transDateModel ]
			});

			$.blockUI();
			$.ajax( {
				type : "POST",
				url : "index.php/admin/report/transferreportData",
				dataType : "json",
				data : {
					fromdate : $("#fromdate").val(),
					todate : $("#todate").val(),
					trans_type:$("#trans_type").val(),
				},
				success : function(response) {
					// Show the grid
					gridOption.data = response.Main;
					gridOption.userData = response.Total;

					$("#jqgrid-table").jqGrid('GridUnload');
					$("#jqgrid-table").jqGrid(gridOption);
					Report.initColumnChooser();

				},
				complete : function() {
					$.unblockUI()
				}
			});
		},
		transfer : function() {
			alert("Chac chan khong vay, tien chu dek phai giay, can than xiu nhe");
			var agree = confirm("Tranfser tien cho thang nay qua LR?");
			if (!agree) {
				return;
			}

			$.blockUI();
			var userId = $(this).attr("data-user-id");
			var amount = $("#amount" + userId).val();

			if (amount == 0) {
				alert("Nhap amount vo");
				return;
			}

			$.ajax( {
				type : "POST",
				url : "index.php/admin/report/transferMoney",
				dataType : "json",
				data : {
					userid : userId,
					amount : amount
				},
				success : function(response) {
					// Show the grid
					alert(response.message);
				},
				complete : function() {
					$.unblockUI()
				}
			});
		},
		bonus : function() {
			alert("Chac chan khong vay, tien chu dek phai giay, can than xiu nhe");
			var agree = confirm("Bonus tien cho thang nay?");
			if (!agree) {
				return;
			}

			$.blockUI();
			var userId = $(this).attr("data-user-id");
			var amount = $("#amount" + userId).val();

			if (amount == 0) {
				alert("Nhap amount vo");
				return;
			}

			$.ajax( {
				type : "POST",
				url : "index.php/admin/report/bonusMoney",
				dataType : "json",
				data : {
					userid : userId,
					amount : amount
				},
				success : function(response) {
					// Show the grid
					alert(response.message);
				},
				complete : function() {
					$.unblockUI()
				}
			});
		}
	};
})(jQuery);
